﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;

namespace Coronox.SkyShell
{
    internal static class SkyDrive
    {
        //internal static string GetSkydriveExePath()
        //{
        //    string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        //    string skydriveExePath = appData + @"\Microsoft\SkyDrive\SkyDrive.exe";
        //    return skydriveExePath;
        //}

        internal static bool IsInstalled()
        {
            if (Directory.Exists(GetSkyDriveSetupFolder()))
                return true;
            else
                return false;
        }


        internal static string GetSkyDriveSetupFolder()
        {
            RegistryKey subkey;

            subkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\SkyDrive");

            if (subkey == null)
            {
                subkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\OneDrive");
            }

            if (subkey == null)
                return "";

            return subkey.GetValue("CurrentVersionPath").ToString();
        }

        internal static string GetSkyDriveUserFolder()
        {
            RegistryKey subkey;

            subkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\SkyDrive");

            if (subkey == null)
            {
                subkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\OneDrive");
            }

            if (subkey == null)
                return "";

            return subkey.GetValue("UserFolder").ToString();
        }


        internal static bool FolderExists(string sourceFolder)
        {
            string targetPath = DetermineTargetPath(sourceFolder);
            return Directory.Exists(targetPath);

        }


        internal static bool SetRegSkyShellJunction(string strSkyDriveFolderName, string strJunctionPath)
        {
            var subkey = Registry.CurrentUser.OpenSubKey("Software\\Coronox\\SkyShellEx\\Junctions", RegistryKeyPermissionCheck.ReadWriteSubTree);
            if (subkey == null)
            {
                subkey = CreateRegistryKeys();
            }

            if (subkey == null)
                return false;

            subkey.SetValue(strSkyDriveFolderName, strJunctionPath);

            return true;
        }


        internal static string GetRegSkyShellJunction(string strSkyDriveFolderName)
        {
            var subkey = Registry.CurrentUser.OpenSubKey("Software\\Coronox\\SkyShellEx\\Junctions", RegistryKeyPermissionCheck.ReadWriteSubTree);

            if (subkey != null)
            {
               var value =       subkey.GetValue(strSkyDriveFolderName);
                if(value != null)
                    return value.ToString();
            }
            return string.Empty;
        }


        internal static bool RemoveRegSkyShellJunction(string strSkyDriveFolderName)
        {
            var subkey = Registry.CurrentUser.OpenSubKey("Software\\Coronox\\SkyShellEx\\Junctions", RegistryKeyPermissionCheck.ReadWriteSubTree);

            if (subkey == null)
                return false;

            if (!string.IsNullOrEmpty(GetRegSkyShellJunction(strSkyDriveFolderName)))
                subkey.DeleteValue(strSkyDriveFolderName);

            return true;
        }



        internal static RegistryKey CreateRegistryKeys()
        {
            var subKey = Registry.CurrentUser.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
            if (subKey == null)
                return null;

            var CoronoxKey = subKey.CreateSubKey("Coronox");
            if (CoronoxKey == null)
                return null;

            var SkyShellExKey = CoronoxKey.CreateSubKey("SkyShellEx");
            if (SkyShellExKey == null)
                return null;

            var JunctionsKey = SkyShellExKey.CreateSubKey("Junctions");
            if (SkyShellExKey == null)
                return null;

            return JunctionsKey;
        }


        internal static string DetermineTargetPath(string sourceFolder)
        {
            DirectoryInfo di = new DirectoryInfo(sourceFolder);
            if (di.Exists)
            {
                string targetName = di.Name;
                string skyDriveFolder = GetSkyDriveUserFolder();
                string targetPath = Path.Combine(skyDriveFolder, targetName);
                return targetPath;
            }
            else
            {
                throw new ArgumentException("Source folder does not exist.");
            }
        }
    }
}
